home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / ODBC for Macintosh / ODBC Tools / SampleSetup / Sources / DialogUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-19  |  9.2 KB  |  490 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    DialogUtilities.c
  3.  *
  4.  *    (c) Apple Computer, Inc 1993
  5.  */
  6.  
  7. #include <Dialogs.h>
  8. #include <DiskInit.h>
  9. #include <Errors.h>
  10. #include <Events.h>
  11. #include <Menus.h>
  12. #include <OSUtils.h>
  13. #include <Packages.h>
  14. #include <Quickdraw.h>
  15. #include <Resources.h>
  16. #include <Script.h>
  17. #include <TextEdit.h>
  18. #include <ToolUtils.h>
  19. #include <Types.h>
  20. #include <Windows.h>
  21.  
  22. #include "DialogUtilities.h"
  23.  
  24. short        DoModalEvent    (ModalFilterProcPtr filterProc);
  25. void        DoMouseDown        (EventRecord *theEvent);
  26. void        DoDiskEvent        (EventRecord *theEvent);
  27. void        DoApp4Event        (EventRecord *theEvent);
  28. void        DoUpdate        (EventRecord *theEvent);
  29. void        DoDrag            (WindowPtr whichWindow, Point where);
  30.  
  31. static Boolean                gFiltered;
  32.  
  33. /*
  34.  * dialog event handling
  35.  */
  36.  
  37. void
  38. MovableDialog(ModalFilterProcPtr filterProc, short *itemHit)
  39. {
  40.     *itemHit = 0;
  41.     if (FrontWindow () != NULL)
  42.     {
  43.         gFiltered = false;
  44.         while (!gFiltered)
  45.         {
  46.             *itemHit = DoModalEvent(filterProc);
  47.         }
  48.     }
  49. }
  50.  
  51. pascal Boolean
  52. StandardFilter(DialogPtr whichDialog, EventRecord *event, short *itemHit)
  53. {
  54.     #define returnKey        13
  55.     #define enter            3
  56.     #define esc                27
  57.     #define pushButton        (ctrlItem + btnCtrl)
  58.     
  59.     Boolean            filtered;
  60.     DialogPeek        whichPeek;
  61.     short            defItem;
  62.     char            key;
  63.     short            itemType;
  64.     Handle            itemHandle;
  65.     Rect            itemRect;
  66.     long            finalTicks;
  67.  
  68.     filtered = false;
  69.     whichPeek = (DialogPeek) whichDialog;
  70.     defItem = whichPeek->aDefItem;
  71.     switch ((*event).what)
  72.     {
  73.         case keyDown:
  74.         case autoKey:
  75.             key = (*event).message & charCodeMask;
  76.             if ((key == returnKey) || (key == enter) || (key == esc)
  77.             || ((key == '.') && (((*event).modifiers & cmdKey) != 0)))
  78.             {
  79.                 if ((key == returnKey) || (key == enter))
  80.                 {
  81.                     *itemHit = defItem;
  82.                 }
  83.                 else
  84.                 {
  85.                     *itemHit = cancel;
  86.                 }
  87.                 GetDItem (whichDialog, *itemHit, &itemType, &itemHandle, &itemRect);
  88.                 if ((itemType & itemDisable) == 0)
  89.                 {
  90.                     if ((itemType & (255 - itemDisable)) == pushButton)
  91.                     {
  92.                         HiliteControl ((ControlHandle) itemHandle, inButton);
  93.                         Delay (8, &finalTicks);
  94.                         HiliteControl ((ControlHandle) itemHandle, 0);
  95.                     }
  96.                     filtered = true;
  97.                 }
  98.             }
  99.             else
  100.             {
  101.                 if ((((*event).modifiers & cmdKey) != 0) &&
  102.                     ((key == 'x') || (key == 'c') || (key == 'v')))
  103.                 {
  104.                     *itemHit = (whichPeek)->editField + 1;
  105.                     switch (key) {
  106.                     case 'x':
  107.                             DlgCut (whichDialog);
  108.                         break;
  109.                     case 'c':
  110.                             DlgCopy (whichDialog);
  111.                             *itemHit = 0;
  112.                         break;
  113.                     case 'v':
  114.                             DlgPaste (whichDialog);
  115.                         break;
  116.                     }
  117.                     if (*itemHit > 0)
  118.                     {
  119.                         GetDItem (whichDialog, *itemHit, &itemType, &itemHandle, &itemRect);
  120.                         if ((itemType & itemDisable) == 0)
  121.                         {
  122.                             filtered = true;
  123.                         }
  124.                     }
  125.                     if (!filtered)
  126.                     {
  127.                         (*event).what = nullEvent;
  128.                     }
  129.                 }
  130.             }
  131.             break;
  132.         
  133.         case updateEvt:
  134.             if ((WindowPtr) (*event).message == qd.thePort)
  135.             {
  136.                 GetDItem (whichDialog, defItem, &itemType, &itemHandle, &itemRect);
  137.                 if ((itemType & (255 - itemDisable)) == pushButton)
  138.                 {
  139.                     OutlineDItem(whichDialog, defItem);
  140.                 }
  141.             }
  142.             else if (!IsDialogEvent (event))
  143.             {
  144.                 DoUpdate(event);
  145.             }
  146.             break;
  147.     }
  148.     
  149.     return (filtered);
  150. }
  151.  
  152. static short
  153. DoModalEvent(ModalFilterProcPtr filterProc)
  154. {
  155.     WindowPtr        eventWindow;
  156.     short            itemHit;
  157.     GrafPtr            savePort;
  158.     DialogPtr        dialog;
  159.     EventRecord        theEvent;
  160.     Boolean            gotEvent;
  161.  
  162.     GetPort(&savePort);
  163.     
  164.     gFiltered = false;
  165.     itemHit = 0;
  166.     dialog = FrontWindow();
  167.     gotEvent = WaitNextEvent (everyEvent, &theEvent, 0, NULL);
  168.     if (gotEvent || (theEvent.what == nullEvent))
  169.     {
  170.         if (theEvent.what == app4Evt)
  171.         {
  172.             DoApp4Event (&theEvent);
  173.         }
  174.         if (IsDialogEvent (&theEvent))
  175.         {
  176.             if ((theEvent.what == activateEvt) || (theEvent.what == updateEvt))
  177.             {
  178.                 eventWindow = (DialogPtr) theEvent.message;
  179.             }
  180.             else
  181.             {
  182.                 eventWindow = FrontWindow ();
  183.             }
  184.             SetPort (eventWindow);
  185.             if (filterProc != NULL)
  186.             {
  187.                 gFiltered = (*filterProc) (dialog, &theEvent, &itemHit);
  188.             }
  189.             else
  190.             {
  191.                 gFiltered = StandardFilter (dialog, &theEvent, &itemHit);
  192.             }
  193.             if (!gFiltered)
  194.             {
  195.                 gFiltered = DialogSelect (&theEvent, &dialog, &itemHit);
  196.             }
  197.         }
  198.         else
  199.         {
  200.             switch (theEvent.what)
  201.             {
  202.                 case mouseDown:
  203.                     DoMouseDown(&theEvent);
  204.                     break;
  205.                 
  206.                 case mouseUp:
  207.                 case keyDown:
  208.                 case autoKey:
  209.                     break;
  210.                 
  211.                 case updateEvt:
  212.                     DoUpdate(&theEvent);
  213.                     break;
  214.                 
  215.                 case activateEvt:
  216.                     break;
  217.                 
  218.                 case diskEvt:
  219.                     DoDiskEvent(&theEvent);
  220.                     break;
  221.             }
  222.         }
  223.     }
  224.     
  225.     SetPort(savePort);
  226.     
  227.     return itemHit;
  228. }
  229.  
  230. static void
  231. DoMouseDown(EventRecord *theEvent)
  232. {
  233.     long            menuChoice;
  234.     short            menuID;
  235.     short            itemNr;
  236.     WindowPtr        whichWindow;
  237.     short            whichPart;
  238.     
  239.     whichPart = FindWindow (theEvent->where, &whichWindow);
  240.     switch (whichPart)
  241.     {
  242.         case inMenuBar:
  243.             menuChoice = MenuSelect (theEvent->where);
  244.             menuID = HiWord (menuChoice);
  245.             itemNr = LoWord (menuChoice);
  246.             if ((menuID != 0) && (itemNr != 0))
  247.             {
  248.                 SysBeep (1);
  249.             }
  250.             break;
  251.         
  252.         case inDrag:
  253.             if (whichWindow == FrontWindow ())
  254.             {
  255.                 DoDrag (whichWindow, theEvent->where);
  256.             }
  257.             else
  258.             {
  259.                 SysBeep (1);
  260.             }
  261.             break;
  262.         
  263.         case inSysWindow:
  264.         case inContent:
  265.         case inDesk:
  266.         case inGrow:
  267.         case inGoAway:
  268.         case inZoomIn:
  269.         case inZoomOut:
  270.         default:
  271.             SysBeep (1);
  272.             break;
  273.     }
  274. }
  275.  
  276. void
  277. DoDrag(WindowPtr whichWindow, Point where)
  278. {
  279.     RgnHandle        grayRgn;
  280.     Rect            limitRect;
  281.     
  282.     grayRgn = GetGrayRgn();
  283.      limitRect = (**grayRgn).rgnBBox;
  284.     DragWindow(whichWindow, where, &limitRect);
  285. }
  286.  
  287. static void
  288. DoDiskEvent(EventRecord *theEvent)
  289. {
  290.     #define dlgTop        75                                   
  291.     #define dlgLeft        100
  292.     
  293.     OSErr    ignore;
  294.     Point    where;
  295.     
  296.     if (HiWord(theEvent->message) != noErr)
  297.     {
  298.         DILoad();
  299.         SetPt(&where, dlgLeft, dlgTop); 
  300.         ignore = DIBadMount(where, theEvent->message);
  301.         DIUnload();
  302.     }
  303. }
  304.  
  305. static void
  306. DoApp4Event(EventRecord */*theEvent*/)
  307. {
  308.     ;
  309. }
  310.  
  311. void
  312. DoUpdate(EventRecord *theEvent)
  313. {
  314.     GrafPtr            savePort;
  315.     WindowPtr        whichWindow;
  316.  
  317.     GetPort (&savePort);
  318.     whichWindow = (WindowPtr) theEvent->message;
  319.     SetPort (whichWindow);
  320.     BeginUpdate (whichWindow);
  321.         ;
  322.     EndUpdate (whichWindow);
  323.     SetPort (savePort);
  324. }
  325.  
  326. /*
  327.  * dialog item handling
  328.  */
  329.  
  330. void
  331. SetDUserItem(DialogPtr dialog, short item, ProcPtr drawProc)
  332. {
  333.     Handle    iHndl;
  334.     Rect    iRect;
  335.     short    iType;
  336.     
  337.     GetDItem(dialog, item, &iType, &iHndl, &iRect);
  338.     SetDItem(dialog, item, iType, (Handle) drawProc, &iRect);
  339. }
  340.  
  341. void
  342. EnableDItem(DialogPtr dialog, short item, Boolean enable)
  343. {
  344.     short        itemType;
  345.     Handle        itemHandle;
  346.     Rect        itemRect;
  347.     
  348.     GetDItem (dialog, item, &itemType, &itemHandle, &itemRect);
  349.     if (enable) {
  350.         itemType &= ~itemDisable;
  351.     } else {
  352.         itemType |= itemDisable;
  353.     }
  354.     SetDItem (dialog, item, itemType, itemHandle, &itemRect);
  355.     
  356.     if ((itemType & ctrlItem) != 0)
  357.     {
  358.         HiliteControl ((ControlHandle) itemHandle, (enable ? 0 : 255));
  359.     }
  360. }
  361.  
  362. void
  363. GetDText(DialogPtr dialog, short item, char *text)
  364. {  
  365.     short    itemType;
  366.     Handle    itemHandle;
  367.     Rect    itemRect;
  368.     
  369.     GetDItem(dialog, item, &itemType, &itemHandle, &itemRect);
  370.     if (itemHandle != nil) GetIText(itemHandle, text);
  371. }
  372.  
  373. void
  374. SetDText(DialogPtr dialog, short item, char *text)
  375. {  
  376.     short    itemType;
  377.     Handle    itemHandle;
  378.     Rect    itemRect;
  379.     char    oldText[256];
  380.     
  381.     GetDItem(dialog, item, &itemType, &itemHandle, &itemRect);
  382.     
  383.     itemType &= 0xFF7F;
  384.     
  385.     switch (itemType)
  386.     {
  387.         case ctrlItem + btnCtrl:
  388.         case ctrlItem + chkCtrl:
  389.         case ctrlItem + radCtrl:
  390.         case ctrlItem + resCtrl:
  391.             GetCTitle((ControlHandle) itemHandle, oldText);
  392.             if (!EqualString(text, oldText, false, false))
  393.                 SetCTitle((ControlHandle) itemHandle, text);
  394.             break;
  395.         
  396.         case statText:
  397.         case editText:
  398.             GetIText(itemHandle, oldText);
  399.             if (!EqualString(text, oldText, false, false))
  400.                 SetIText(itemHandle, text);
  401.             break;
  402.     }
  403. }
  404.  
  405. void
  406. SetDRadioButton(DialogPtr dialog, short theBtn, short firstBtn, short lastBtn)
  407. {  
  408.     short    iType, i;
  409.     Handle    iHdl;
  410.     Rect    iRect;
  411.     
  412.     for (i = firstBtn; i <= lastBtn; i++)
  413.     {
  414.         GetDItem(dialog, i, &iType, &iHdl, &iRect);
  415.         if (iHdl != nil) SetCtlValue((ControlHandle) iHdl, theBtn ==  i ? 1 : 0);
  416.     }
  417. }
  418.  
  419. short
  420. GetDRadioButton(DialogPtr dialog, short firstBtn, short lastBtn)
  421. {  
  422.     short    iType, i;
  423.     Handle    iHdl;
  424.     Rect    iRect;
  425.     
  426.     for (i = firstBtn; i <= lastBtn; i++)
  427.     {
  428.         GetDItem(dialog, i, &iType, &iHdl, &iRect);
  429.         if (iHdl != nil)
  430.             if (GetCtlValue((ControlHandle) iHdl)) break;
  431.     }
  432.     return i;
  433. }
  434.  
  435. void
  436. OutlineDItem(DialogPtr dialog, short item)
  437. {
  438.     PenState    savePen;
  439.     Handle        iHndl;
  440.     Rect        iRect;
  441.     short        iType;
  442.     short        curveFact;
  443.     
  444.     GetDItem(dialog, item, &iType, &iHndl, &iRect);
  445.     
  446.     GetPenState(&savePen);
  447.     PenNormal();
  448.     PenSize(3, 3);
  449.     InsetRect(&iRect, -4, -4);
  450.     curveFact = (iRect.bottom - iRect.top + 8) / 2;    
  451.     FrameRoundRect(&iRect, curveFact, curveFact);
  452.     SetPenState(&savePen);
  453. }
  454.  
  455. pascal void
  456. DrawItemLineGray(DialogPtr dialog, short item)
  457. {
  458.     Handle    item_handle;
  459.     Rect    item_rect;
  460.     short    item_type;
  461.     
  462.     GetDItem(dialog, item, &item_type, &item_handle, &item_rect);
  463.     
  464.     if ((item_rect.right - item_rect.left) > (item_rect.bottom - item_rect.top))
  465.     {
  466.         PenPat(&qd.gray);
  467.         MoveTo(item_rect.left, item_rect.top);
  468.         LineTo(item_rect.right - 1, item_rect.top);
  469.         PenPat(&qd.black);
  470.     }
  471.     else
  472.     {
  473.         PenPat(&qd.gray);
  474.         MoveTo(item_rect.left, item_rect.top);
  475.         LineTo(item_rect.left, item_rect.bottom - 1);
  476.         PenPat(&qd.black);
  477.     }
  478. }
  479.  
  480. pascal void
  481. DrawItemRect(DialogPtr dialog, short item)
  482. {
  483.     Handle    item_handle;
  484.     Rect    item_rect;
  485.     short    item_type;
  486.     
  487.     GetDItem(dialog, item, &item_type, &item_handle, &item_rect);
  488.     FrameRect(&item_rect);
  489. }
  490.